package com.maya.medicalwastes.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.maya.medicalwastes.App;
import com.maya.medicalwastes.CollectActivity;
import com.maya.medicalwastes.ParamConstant;
import com.maya.medicalwastes.R;
import com.maya.medicalwastes.adapter.KeyAdapter;
import com.maya.medicalwastes.bean.QueryInfoBean;
import com.maya.medicalwastes.entity.CollectBean;
import com.maya.medicalwastes.entity.KeyBean;
import com.maya.medicalwastes.httpNet.ApiService;
import com.maya.medicalwastes.httpNet.HttpService;
import com.maya.medicalwastes.httpNet.callback.ResponseCallBack;
import com.maya.medicalwastes.httpNet.common.SharedPreferencesConstant;
import com.maya.medicalwastes.httpNet.manage.RetrofitManager;
import com.maya.medicalwastes.httpNet.model.ResultModel;
import com.maya.medicalwastes.util.FormatUtilA;
import com.maya.medicalwastes.util.MyUtils;
import com.maya.medicalwastes.util.PrefUtils;
import com.maya.medicalwastes.util.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

public class BarcodeActivity extends AppCompatActivity {

    @BindView(R.id.btn_do)
    Button btn_do;
    @BindView(R.id.tv_type)
    Spinner tv_type;
    @BindView(R.id.tv_look_recoode)
    TextView tv_look_recoode;
    @BindView(R.id.et_num)
    EditText et_num;
    @BindView(R.id.tv_title)
    TextView tv_title;
    private String type = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_barcode);
        ButterKnife.bind(this);
        tv_title.setText("二维码生成");
        KeyAdapter mCollectBeanArrayAdapter = new KeyAdapter(this, App.getInstance().getWareType());
        tv_type.setAdapter(mCollectBeanArrayAdapter);
        btn_do.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!et_num.getText().toString().equals("") && !type.equals("")) {
                    getbarcode();
                } else {
                    ToastUtils.ViewToast(BarcodeActivity.this, "请选择分类和输入生成数量");
                    return;
                }
            }
        });


        tv_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                KeyBean mItem = (KeyBean) mCollectBeanArrayAdapter.getItem(i);
                type = mItem.getKey();
                tv_type.setSelection(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
        tv_look_recoode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              //  MyUtils.IntentToOther(BarcodeActivity.this,BarcodeDetailActivity.class,null);
            }
        });
    }


    public void getbarcode() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", App.getInstance().getUserEntity().getId());
        params.put("userName", App.getInstance().getUserEntity().getUserName());
        params.put("generateNum", et_num.getText().toString());
        params.put("generateType", type);
        Observable<ResultModel<String>> call = RetrofitManager.getInstance().getRetrofit().create(ApiService.class).barcode(params, PrefUtils.getString(this, SharedPreferencesConstant.TOKEN, ""));
        HttpService.getInstance().enqueue(call, new ResponseCallBack<ResultModel<String>>() {
            @Override
            public void onResponse(ResultModel<String> response) {
                if (response != null && response.httpCode == 200) {
              //  ToastUtils.ViewToast(BarcodeActivity.this,"生成二维码成功");
                Bundle mBundle =new Bundle();
                mBundle.putString("data",response.data);
                }
            }

            @Override
            public void onFailure(int httpCode) {
                //   closeDialog();
            }
        }, this);
    }

}
